package demo.xiaxl.com.android_demo.pagersnaphelper;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PagerSnapHelper;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Random;

import demo.xiaxl.com.android_demo.R;

public class PagerSnapHelperActivity extends Activity {

    /**
     * UI
     */
    // recycleView
    private FixRecyclerView rvContent = null;
    private ListView lvTitle = null;
    private ListView currentLv = null;

    // adapter
    private ContentAdapter mMyadapter = null;
    /**
     * 数据
     */
    //data
    ArrayList<DataBean> datas = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycle_pager_activity);

        // 纵向List
        initUI();
        initData();
    }

    private void initData() {
        Random random = new Random();
        for (int i = 1; i < 6; i++) {
            DataBean data = new DataBean();
            data.title = "title " + i;
            data.color = 0xff000000 | random.nextInt(0x00ffffff);
            int ran = random.nextInt(40);
            data.list = new ArrayList<>();
            for (int j = 1; j < ran + 1; j++) {
                data.list.add("item" + j);
            }
            datas.add(data);
        }

        ArrayList<String> res = new ArrayList<>();
        for (DataBean bean : datas) {
            res.add(bean.title);
        }
        TitleAdapter titleAdapter = new TitleAdapter(res, lvTitle);
        lvTitle.setAdapter(titleAdapter);
        titleAdapter.notifyDataSetChanged();
    }

    int visiblePos = -1;

    public void initUI() {
        rvContent = findViewById(R.id.rvContent);
        rvContent.setNestedScrollingEnabled(false);
        lvTitle = findViewById(R.id.lvTitle);

        final PagerSnapHelper snapHelper = new PagerSnapHelper();
        snapHelper.attachToRecyclerView(rvContent);
        // ---布局管理器---
        final LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this) {
//            @Override
//            public boolean canScrollVertically() {
//                return false;
//            }
        };
        // 默认是Vertical (HORIZONTAL则为横向列表)
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rvContent.setLayoutManager(linearLayoutManager);
        rvContent.setCanScrollListener(new FixRecyclerView.Scrollable() {
            @Override
            public boolean canContentScrollUp() {
                View view = snapHelper.findSnapView(linearLayoutManager);
                if (view != null) {
                    currentLv = view.findViewById(R.id.lv);
                    if (currentLv != null) {
                        return !isListViewReachBottomEdge(currentLv);
                    }
                }
                return false;
            }

            @Override
            public boolean canContentScrollDown() {
                View view = snapHelper.findSnapView(linearLayoutManager);
                if (view != null) {
                    currentLv = view.findViewById(R.id.lv);
                    if (currentLv != null) {
                        return !isListViewReachTopEdge(currentLv);
                    }
                }
                return false;
            }
        });
        // TODO 这么写是为了获取RecycleView的宽高
        rvContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                    rvContent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    rvContent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }

                /**
                 *  这么写是为了获取RecycleView的宽高
                 */
                // 创建Adapter，并指定数据集
                mMyadapter = new ContentAdapter(datas, rvContent.getWidth(), rvContent.getHeight());
                // 设置Adapter
                rvContent.setAdapter(mMyadapter);
            }
        });

    }

    public boolean isListViewReachTopEdge(final ListView listView) {
        boolean result = false;
        if (listView.getFirstVisiblePosition() == 0) {
            final View topChildView = listView.getChildAt(0);
            result = topChildView.getTop() == 0;
        }
        return result;
    }

    public boolean isListViewReachBottomEdge(final ListView listView) {
        boolean result = false;
        if (listView.getLastVisiblePosition() == (listView.getCount() - 1)) {
            final View bottomChildView = listView.getChildAt(listView.getLastVisiblePosition() - listView.getFirstVisiblePosition());
            result = (listView.getHeight() >= bottomChildView.getBottom());
        }
        ;
        return result;
    }
}
